package com.huajiao.management_system.controller;

import com.huajiao.management_system.bean.competitionRes;
import com.huajiao.management_system.bean.prove;
import com.huajiao.management_system.service.competitionResService;
import com.huajiao.management_system.service.proveService;
import com.huajiao.management_system.util.PageResult;
import com.huajiao.management_system.util.Result;
import com.huajiao.management_system.util.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("/competition")
@Api(tags = "竞赛成果相关接口")
public class competitionResController {
    @Resource
    private competitionResService competitionresService;
    @Resource
    private proveService proveService;
    @PostMapping("/list")
    @ApiOperation(value = "分页查询竞赛成果", notes="根据一定的条件分页查询竞赛成果")
    @ResponseBody
    public PageResult<competitionRes> list(String Time,String rank, String keyword, Integer page) {
        return competitionresService.selectPage(Time,rank,keyword,page);
    }

    @PostMapping("/save")
    @ApiOperation(value = "添加竞赛成果", notes="添加竞赛成果")
    @ResponseBody
    public Result save(competitionRes competitionres, @RequestParam("files") MultipartFile[] files) {
        competitionres.setCreateTime(LocalDateTime.now());
        if (competitionresService.insertOne(competitionres) == 0) {
            return new Result("添加失败！");
        }
        if (isUpload(competitionres, files)) {
            return new Result("图片上传失败！");
        }
        return new Result("添加成功！");
    }

    /**
     * url是需要删除图片的路径集合，files是新上传的图片
     * @param competitionres
     * @param url
     * @param files
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改竞赛成果", notes="修改竞赛成果")
    @ResponseBody
    public Result update(competitionRes competitionres,String[ ] url,@RequestParam("files") MultipartFile[] files) {
        competitionres.setUpdateTime(LocalDateTime.now());
        if (competitionresService.updateOne(competitionres) == 0) {
            return new Result("信息修改失败！");
        }
        if (isUpload(competitionres, files)) {
            return new Result("图片上传失败！");
        }
        if (url.length != 0) {
            if (proveService.deleteImage(url)) {
                return new Result("修改成功！");
            } else {
                return new Result("修改失败！");
            }
        }else {
            return new Result("未删除图片!");
        }
    }



    @PostMapping("/delete")
    @ApiOperation(value = "批量删除竞赛成果", notes="根据id数组批量删除")
    @ResponseBody
    public Result  delete( Integer[] ids) {
        if (ids.length < 1) {
            return new Result("参数异常！");
        }
        if (competitionresService.deleteBatch(ids)) {
            return new Result("删除成功！");
        } else {
            return new Result( "删除失败");
        }
    }

    /**
     * 不分页的条件查询
     * @param Time
     * @param rank
     * @param keyword
     * @return
     */
    @GetMapping("/all")
    @ApiOperation(value = "获取所有竞赛成果", notes="获取所有竞赛成果")
    @ResponseBody
    public List<competitionRes> getAll(String Time, String rank, String keyword){
        return competitionresService.getAll(Time, rank, keyword);
    }

    private boolean isUpload(competitionRes competitionres, MultipartFile[] files) {
        if (files != null) {
            List<prove> list = UploadUtil.upload(files, competitionres.getTypeName(), competitionres.getCompetitionResName(), competitionres.getCompetitionResId(), competitionres.getTypeId());
            if (list != null) {
                for (prove pro : list) {
                    if (proveService.insertOne(pro) == 0) {
                        System.out.println("====================");
                        System.out.println(pro.getProveUrl());
                        return true;
                    }
                }
            }
        }
        return false;
    }


}

